/******************************************************************************
 * Copyright (C) ShenZhen Powerdata Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳市博安达信息技术股份有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 *****************************************************************************/

package com.szboanda.hbdc.workflow.dynamicform.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.szboanda.beijing.pure.BaseBusinessService;
import com.szboanda.component.log.cmommon.utils.JsonHelper;
import com.szboanda.dynamicform.entity.Form;
import com.szboanda.dynamicform.entity.FormFactory;
import com.szboanda.dynamicform.viewresolver.exception.ViewResolverException;
import com.szboanda.dynamicform.viewresolver.service.IViewResolverService;
import com.szboanda.hbdc.gwgl.fwgl.exception.GwglFwglException;
import com.szboanda.hbdc.workflow.dynamicform.dao.WorkflowDynamicformDAO;
import com.szboanda.hbdc.workflow.dynamicform.exception.WorkflowDynamicformException;
import com.szboanda.hbdc.workflow.dynamicform.service.IWorkflowDynamicformService;
import com.szboanda.platform.common.utils.LoggerUtil;
import com.szboanda.platform.common.utils.MapUtils;


/**
 * @title: 流程动态表单_业务处理service实现类
 * @fileName: WorkflowDynamicformImpl.java
 * @description:
 * @copyright: PowerData Software Co.,Ltd. Rights Reserved.
 * @company: 深圳市博安达信息技术股份有限公司
 * @author: liuzz
 * @date: 2021-3-11 20:45:55
 * @version: V1.0
 */
@Service("WorkflowDynamicformImpl")
public class WorkflowDynamicformImpl extends BaseBusinessService implements IWorkflowDynamicformService {

	/**
	 * 注入业务处理DAO
	 */
    @Autowired
    private WorkflowDynamicformDAO workflowDynamicformDAO;
    
    /**
     * 视图解析器service 业务实现
     */
    @Autowired
    public IViewResolverService viewResolverService;

    /**
     * 修改【流程动态表单】业务信息
     * @param workflowId 流程定义序号
     * @param businessId 业务序号
     * @param model
     * @return
     * @throws WorkflowDynamicformException
     */
    @Override
    public void updateBusiness(String workflowId, String businessId, Map<String, Object> model) throws GwglFwglException {
    	Map<String,Object> gxInfo = workflowDynamicformDAO.getFormIdByWorkflowId(workflowId);
    	String formId = MapUtils.getString(gxInfo, "BDMBXH");
    	
    	Form form = FormFactory.getInstance().getForm(formId);
    	String tableName = form.getFormTable();
    	
    	Map<String,Object> saveModel = new HashMap<String,Object>();

    	Map<String,Object> formModel = new HashMap<String,Object>();
    	model.put("MBBH", formId);
    	model.put("XH", businessId);
    	model.put("MBBH", formId);
    	formModel.put(tableName, model);
    	formModel.put("template_id", formId);
    	formModel.put("record_id", businessId);
    	formModel.put("client_type", "WorkflowDynamicform");
    	
        saveModel.put("recordInfo", JsonHelper.parseObject(formModel));
        saveModel.put("recodeId", businessId);
        saveModel.put("templateId", formId);
        saveModel.put("clientType", "WorkflowDynamicform");
        saveModel.put("ywcldybh", null);
        
        try {
			viewResolverService.saveForm(saveModel);
		} catch (ViewResolverException e) {
			String errorMsg = "流程中存储业务信息失败！";
			LoggerUtil.error(this.getClass(), errorMsg + "workflowId={}，businessId={}，model={}", workflowId, businessId, model);
			throw new WorkflowDynamicformException(errorMsg, e);
		}
    }

}
